<?php
/**
 * Main file from PUF Library
 *
 * Loads and initializes all files required to work 
 */
namespace PUF;

/**
 * Require the global functions and constants
 */
require_once 'Bootstrap.php';

/**
 * Require the loader, to start loading other classes
 */
require_once 'Loader.php';

/**
 * Set timezone
 */
date_default_timezone_set('UTC');

/**
 * Load the required classes
 */
$Loader = Loader::getInstance();
$Loader->load(['Route', 'Router', 'Controller', 'Plugin',
	'Database', 'Model', 'DatabaseIterator'
]);

/**
 * Load the routes set by the user
 */
$Loader->load(['routes', 'database', 'bootstrap'], ['type' => 'config']);

/**
 * Load selected controller (defined by the url)
 */
$Router = Router::getInstance();
$controllerName = $Router->getController();
$controllerOptions = ['type' => 'controller'];

if ($plugin = $Router->getPlugin()) {
	$controllerOptions['plugin'] = $plugin->getName();
	$controllerClassName = "PUF\\Plugin\\{$controllerName}";
} else {
	$controllerClassName = "PUF\\{$controllerName}";
}

$Loader->load($controllerName, $controllerOptions);
$Controller = new $controllerClassName();

/**
 * Call selected method
 */
$Controller->setParams($Router->getParams());
$Controller->setMethod($Router->getMethod());
$Controller->_execute();
